<template>
	<view class="app">
		<view class="min">
			<view class="title">
				<text class="title_text">我的LHT二维码</text>
			</view>
			<view class="mid">
				<canvas canvas-id="qrcode" class="canvas-hide" />
				<image class="qrcode" :src="QRCode64"  @touchstart.native="showDelModal()" @touchend.native="clearLoop()" ></image>
				<text class="qrcode_text">长按保存至相册</text>
			</view>
			<view class="title">
				<text class="title_text">我的LHT地址链接</text>
			</view>
			<view class="address">
				<view class="address_text">{{address}}</view>
				<view class="address_copy" @click="copyValue">复制</view>
			</view>
		</view>
	</view>
</template>

<script>
	import uQRCode from '@/components/Sansnn-uQRCode/uqrcode.js'
	
	let that;
	
	export default {
		name: 'Index',
		components: {	},
		data() {
			return {
				address:"未知",
				QRCode64:null,
			};
		},
		methods: {
			/**
			 * 查询用户基本信息
			 */
			async queryUser(){
				await this.$request('user', 'get',{
				}).then(res=>{
					console.log("查询用户信息",res);
					that.userDetail = res.data;
				}).catch(err=>{
					console.error(err);
				})
			},
			
			// 复制地址
			copyValue(){
				uni.setClipboardData({
				    data: that.address,
				    success: function () {
				       this.$util.msg('复制成功');
				    }
				});
			},
			
			//生成二维码方法
			async makeQRCode(text){
				// 返回图片base64
				var res = await uQRCode.make({
				        canvasId: 'qrcode',
				        componentInstance: this,
				        text: text,
				        size: 354,
				        margin: 10,
				        backgroundColor: 'rgba(255,255,255,0)',
				        foregroundColor: '#000000',
				        fileType: 'png',
				        correctLevel: uQRCode.errorCorrectLevel.H
				      });
				this.QRCode64 = res;
				console.log(res);
			},
			
			showDelModal(){
				clearTimeout(this.Loop); //再次清空定时器，防止重复注册定时器
				this.Loop = setTimeout(function() {
					uni.saveImageToPhotosAlbum({
						filePath: this.QRCode64,
						success: function () {
							this.$util.msg("保存成功");
						}
					});
					
				}.bind(this), 1000);
			},
			clearLoop(e) {
				clearTimeout(this.Loop);
			}
		},
		// 监听页面加载
		onLoad(event){
			that = this;
			if(event.address){
				that.address = event.address;
				// 生成二维码
				that.makeQRCode(that.address);
			}
			
		},
		// 监听页面显示
		onShow() {
			
			
		},
		// 监听页面隐藏
		onHide(){
			
		},
		// 监听页面卸载
		onUnload(){
			
		},
		// 监听页面下拉刷新
		onPullDownRefresh(){
			
		},
		// 组件：在实例创建完成后被立即调用
		created() {
			
		},
	};
	
</script>


<style>
	/* 相对路径 */
	@import url('./myAddress.css');
</style>
